(function (window) {
    var nvcVal;
    var aiCaptcha = function () {
    };

    /** @plugin 事件驱动器 */
    var events = {
        /**
         * 注册事件
         * @param {string} en 事件名称
         * @param {aiCaptcha~nvcCallback} cb 事件回调
         * @param {string} [idx] 回调索引
         */
        on: function (en, cb, idx) {
            if (!this.checkIsEvent(en) || typeof cb !== "function") {
                return;
            }
            if (typeof idx === "string") {
                this[en][idx] = cb;
            } else {
                this[en].push(cb);
            }
        },
        /**
         * 注销事件
         * @param {string} en 事件名称
         * @param {callback|string} cbOrIdx 事件回调/索引
         * @return boolean
         */
        off: function (en, cbOrIdx) {
            if (!this.checkIsEvent(en)) {
                return false;
            }
            if (typeof cbOrIdx === "string") {
                if (this[en].hasOwnProperty(cbOrIdx)) {
                    this[en][cbOrIdx] = undefined;
                }
            } else if (typeof cbOrIdx === "function") {
                for (var i in this[en]) {
                    if (this[en].hasOwnProperty(i) && this[en][i] === cbOrIdx) {
                        this[en][i] = undefined;
                    }
                }
            } else {
                return false;
            }
            return true;
        },
        /**
         * 触发事件
         * @param {string} en 事件名称
         * @param {object} data 事件数据
         */
        trigger: function (en, data) {
            if (!this.checkIsEvent(en)) {
                return;
            }
            for (var i in this[en]) {
                if (this[en].hasOwnProperty(i) && typeof this[en][i] === "function") {
                    this[en][i](data);
                }
            }
        },
        /**
         * 检查是否事件
         * @param {string} en 事件名称
         * @return {boolean}
         */
        checkIsEvent: function (en) {
            return this.hasOwnProperty(en) && this[en] instanceof window.Array;
        },
        loaded: [],
        nvcCallback: [],
    };

    aiCaptcha.prototype.init = function (renderTo, opts) {
        if (opts === undefined) {
            opts = {};
        }
        //FFFF0N00000000007E0E
        var nvcOpt = {
            NVC_APP_KEY: "CF_APP_1",
            // trans: {'nvcCode':400, key1: 'code0'},
            scene: "nvc_register",
            popUp: false,
            renderTo: renderTo,
            width: opts.width,
            height: opts.height,
            nvcCallback: function (data) {
                events.trigger('nvcCallback', data);
                window.console.log(data);
                nvcVal = data;
            }
        };
        nvcOpt.NVC_APP_KEY = 'FFFF0N00000000007E0E';
        nvcOpt.scene = 'ic_message';
        window.nvcInit(nvcOpt);
    };

    aiCaptcha.prototype.getNVCVal = function () {
        return nvcVal || getNVCVal();
    };
    /**
     * @param {aiCaptcha~nvcCallback} [cb]
     */
    aiCaptcha.prototype.getNC = function (cb) {
        getNC().then(function(){
            _nvc_nc.upLang('cn', {
                _startTEXT: "请按住滑块，拖动到最右边",
                _yesTEXT: "验证通过",
                _error300: "出错了，点击<a href=\"javascript:__nc.reset()\">刷新</a>再来一次",
                _errorNetwork: "网络不给力，请<a href=\"javascript:__nc.reset()\">点击刷新</a>",
            })
            _nvc_nc.reset();
        });
        this.onSecondValidate(cb);
    };
    aiCaptcha.prototype.reset = function () {
        nvcReset();
        typeof _nvc_nc !== "undefined" && _nvc_nc.reset();
    };

    /**
     * @callback {aiCaptcha~nvcCallback}
     * @param {object} data
     */
    /**
     * @param {aiCaptcha~nvcCallback} cb
     */
    aiCaptcha.prototype.onSecondValidate = function (cb) {
        events.on('nvcCallback', cb);
    };
    window.aiCaptchaPlugin = new aiCaptcha();
})(window);
